import nltk.tokenize
import re
import string
from nltk.stem import PorterStemmer
from nltk.corpus import wordnet

def ReplaceRepeatedLetters(word):  
    repeat_regexp = re.compile(r'(\w*)(\w)\2(\w*)')
    repl = r'\1\2\3'  
    if wordnet.synsets(word):
      return word
    repl_word = repeat_regexp.sub(repl, word)
    if repl_word != word:
      return ReplaceRepeatedLetters(repl_word)
    else:
      return repl_word

def ReplaceEmoticons(tweet):
    tweet = tweet.replace(":)","happy")
    tweet = tweet.replace(":))","happy")
    tweet = tweet.replace(";)","happy")
    tweet = tweet.replace(":(","sad")
    tweet = tweet.replace(";(","sad")
    tweet = tweet.replace(":((","sad")
    tweet = tweet.replace(":'(","sad")
    tweet = tweet.replace(":-)", "happy")
    tweet = tweet.replace("=)", "happy")
    tweet = tweet.replace(":D", "happy")
    tweet = tweet.replace(":-D", "happy")
    tweet = tweet.replace(":-(","sad")
    tweet = tweet.replace("=(","sad")
    return tweet
         
def ReplacePunctuation(tweet):
    tweet = re.sub("\?{1,}!{1,}", " question ", tweet)
    tweet = re.sub("!{1,}\?{1,}", " question ", tweet)
    tweet = re.sub("\?{1,}", " question ", tweet)
    tweet = re.sub("!{1,}", " exclamation ", tweet)        
    for c in string.punctuation:
        tweet = tweet.replace(c,"")        
    return tweet
    
def ReplacePointers(tweet):
    return re.sub("@[^\s][\w]*", " pointer ", tweet)    

def ReplaceUrl(tweet):
    return re.sub("([A-Za-z]{3,9})://([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((/[-\+~%/\.\w]+)?\??([-\+=&;%@\.\w]+)?#?([\w]+)?)", " URL ", tweet)     

def ApplySteeming(tweet):
    stemmer = PorterStemmer()
    return stemmer.stem(tweet)


